# -*- coding: utf-8 -*-

from odoo import models, fields, api,_
from odoo.exceptions import ValidationError


class EmployeeExpensePaymentBalance(models.Model):
    _name = 'employee_expense_payment_balance'

    sheet_id=fields.Many2one('hr.expense.sheet')
    advance_payment_id=fields.Many2one('expense.advance.payment')
    amount=fields.Float(compute='compute_amount')

    def compute_amount(self):
        for r in self:
            if r.sheet_id:
                r.amount=r.sheet_id.total_amount
            else:
                r.amount=-r.advance_payment_id.amount

    state=fields.Char(compute='compute_state',store=True)
    @api.depends('sheet_id.state','advance_payment_id.state')
    def compute_state(self):
        for r in self:
            if r.sheet_id.state=='paid':
                r.state='done'
            elif r.advance_payment_id.state=='done':
                r.state='done'
            else:
                r.state='draft'

    apply_date = fields.Date(compute='compute_apply_date', store=True)

    @api.depends('sheet_id.apply_date', 'advance_payment_id.apply_date')
    def compute_apply_date(self):
        for r in self:
            if r.sheet_id:
                r.apply_date=r.sheet_id.apply_date
            elif r.advance_payment_id:
                r.apply_date = r.advance_payment_id.apply_date

    name = fields.Char(compute='compute_name', store=True)

    @api.depends('sheet_id.name', 'advance_payment_id.name')
    def compute_name(self):
        for r in self:
            if r.sheet_id:
                r.name = r.sheet_id.name
            elif r.advance_payment_id:
                r.name = r.advance_payment_id.name
